/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import core.SchoolMat;
import core.TestData;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import javax.ejb.Stateless;

/**
 *
 * @author Lukin
 */
@Stateless
public class SchoolMatDAO implements ISchoolMat {

    @Override
    public void createSchoolMat(SchoolMat mat) {
        if (mat == null) {
            throw new RuntimeException("null paprametr");
        } else {
            Integer userId = this.getMaxSchoolMatId();
            mat.setId(userId);
            TestData.MATS_TABLE.put(userId, mat);
        }
    }

    @Override
    public void updateSchoolMat(SchoolMat mat) {
 if(mat == null || !TestData.MATS_TABLE.containsKey(mat.getId()))
        {
            throw new RuntimeException("Uživatelské jméno je již obsazené" );
        }
        TestData.MATS_TABLE.put(mat.getId(), mat);    
    }

    @Override
    public void deleteSchoolMat(SchoolMat mat) {
 if(mat == null)
        {
            throw new RuntimeException("null");
        }
        TestData.MATS_TABLE.remove(mat.getId());    }

    @Override
    public SchoolMat findSchoolMat(int matId) {
        return TestData.MATS_TABLE.get(matId);
    }

    @Override
    public SchoolMat findSchoolMat(String username) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public List<SchoolMat> findAllSchoolMats() {
       return new ArrayList<SchoolMat>(TestData.MATS_TABLE.values());
    }

    private Integer getMaxSchoolMatId() {
        Set<Integer> keys = TestData.MATS_TABLE.keySet();
        Integer maxId = 1;
        for (Integer key : keys) {
            if (key > maxId) {
                maxId = key;
            }
        }
        return maxId;
    }
}
